﻿using STT.Business;
using STT.Layer.Connection;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace STT.Admin
{
    public partial class login : System.Web.UI.Page
    {
        BTbDangNhap dn = new BTbDangNhap();
        Data db = new Data();
        ToolBox tl = new ToolBox();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Cookies["userInfo"] != null)
            {
                //Response.Redirect("~/Admin/Default.aspx");
                LoadSessionByCookies();
            }
        }
        void ThongBao(string Loi)
        {
            ScriptManager.RegisterClientScriptBlock(this, GetType(), "Thông báo!", "<script>alert('" + Loi + "')</script>", false);
        }
        protected void btDangNhap_Click(object sender, EventArgs e)
        {
            using (SqlConnection cn = new SqlConnection(db.str))
            {
                db.OpenConnection(cn);
                DataTable dt = db.GetDataTableById2("[GetTkDangNhapByTenDN]", txtTenDangNhap.Text);
                if (dt.Rows.Count > 0)
                {
                    if (dt.Rows[0]["Khoataikhoan"].ToString() == "0" && dt.Rows[0]["TrangThai"].ToString()=="0")
                    {
                        if (dt.Rows[0]["MatKhau"].ToString().Trim() == dn.GetMD5(txtMatKhau.Text.Trim() + dt.Rows[0]["RadomPass"].ToString().Trim()))
                        {
                            if (db.GetDataTableById2("GiangVien_GetByMaDN", txtTenDangNhap.Text).Rows.Count > 0)
                            {

                                if (dt.Rows[0]["Quyen"].ToString() == "4" || dt.Rows[0]["Quyen"].ToString() == "5")
                                {

                                    ThongBao("Tài khoản hoặc mật khẩu sai....!");
                                }
                                else
                                {
                                    if (cbNhoMatKhau.Checked == true)
                                    {
                                        Response.Cookies["userInfo"]["userName"] = txtTenDangNhap.Text;
                                        Response.Cookies["userInfo"]["pass"] = txtMatKhau.Text;
                                        Response.Cookies["userInfo"].Expires = DateTime.Now.AddDays(15);
                                    }
                                    if (cbNhoMatKhau.Checked == false)
                                    {
                                        Response.Cookies["userInfo"]["userName"] = txtTenDangNhap.Text;
                                        Response.Cookies["userInfo"]["pass"] = txtMatKhau.Text;
                                        Response.Cookies["userInfo"].Expires = DateTime.Now.AddDays(1);
                                    }
                                    DataTable admin = tl.GetDataTableById2("GiangVien_GetByMaDN", txtTenDangNhap.Text);
                                    Session["QuyenDN"] = dt.Rows[0]["Quyen"].ToString().Trim();
                                    Session["name"] = txtTenDangNhap.Text;
                                    Session["TenDN"] = dt.Rows[0]["TenDN"].ToString().Trim();
                                    Session["TenNguoiDung"] = admin.Rows[0]["TenGV"].ToString().Trim();

                                    if (admin.Rows[0]["Anh"].ToString().Trim() == "")
                                    {
                                        Session["Anh"] = "";
                                    }
                                    else
                                    {
                                        Session["Anh"] = admin.Rows[0]["Anh"].ToString().Trim();
                                    }
                                    Response.Redirect("~/Admin/Default.aspx?idpage=1");
                                }
                            }
                            else
                            {
                                ThongBao("Tài khoản hoặc mật khẩu sai....!");

                            }
                        }
                        else
                        {
                            ThongBao("Tài khoản hoặc mật khẩu sai....!");

                        }
                    }
                    else
                    {
                        ThongBao("Tài khoản đã bị khóa....!");
                    }
                }
                else
                {
                    ThongBao("Tài khoản hoặc mật khẩu sai....!");
                }
            }
        }
        private void LoadSessionByCookies()
        {
            DataTable dt = db.GetDataTableById2("[GetTkDangNhapByTenDN]", Server.HtmlDecode(Request.Cookies["userInfo"]["userName"]));
            DataTable admin = tl.GetDataTableById2("GiangVien_GetByMaDN", Server.HtmlDecode(Request.Cookies["userInfo"]["userName"]));
            Session["QuyenDN"] = dt.Rows[0]["Quyen"].ToString().Trim();
            Session["name"] = txtTenDangNhap.Text;
            Session["TenDN"] = dt.Rows[0]["TenDN"].ToString().Trim();
            Session["TenNguoiDung"] = admin.Rows[0]["TenGV"].ToString().Trim();
            Session["Anh"] = admin.Rows[0]["Anh"].ToString().Trim();
            Response.Redirect("~/Admin/Default.aspx?idpage=1");
        }
    }
}